#ifndef UART_FORWARD_H
#define UART_FORWARD_H

#include "main.h"
#include "ring_buffer.h"

#define PACKET_DATA_SIZE    9   
#define PACKET_TOTAL_SIZE   10 

typedef enum {
    FORWARD_STATE_IDLE,         
    FORWARD_STATE_RECEIVING,   
    FORWARD_STATE_PROCESSING,   
    FORWARD_STATE_SENDING      
} ForwardState_t;

typedef struct {
    UART_HandleTypeDef *huart;          
    RingBuffer_t rx_ring;               
    uint8_t rx_byte;                    
    uint8_t tx_buffer[PACKET_TOTAL_SIZE]; 
    volatile bool tx_busy;              
    volatile ForwardState_t state;      
    uint32_t rx_timeout_ms;             
    uint32_t last_rx_tick;              
    
    uint32_t packets_received;          
    uint32_t packets_sent;              
    uint32_t crc_errors;                
    uint32_t buffer_overflows;          
} UartChannel_t;

void uart_forward_init(void);
void uart_forward_process(void);
void uart_forward_start_receive(void);
void uart_forward_stop_receive(void);

void uart_forward_get_stats(uint32_t *uart1_rx, uint32_t *uart1_tx, 
                           uint32_t *uart3_rx, uint32_t *uart3_tx);

void uart_forward_clear_errors(void);

void uart_forward_rx_callback(UART_HandleTypeDef *huart);
void uart_forward_tx_callback(UART_HandleTypeDef *huart);
void uart_forward_error_callback(UART_HandleTypeDef *huart);

#endif /* UART_FORWARD_H */